package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.StrategyCommentObjcet;
import cn.wolfcode.luowowo.query.StrategyObjcet;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @GetMapping("/query")
    public Object query(StrategyObjcet qo){
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }
    @GetMapping("/detail")
    public Object detail(String id){
        strategyStatisVORedisService.viewnumIncrease(id,1);
        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }
    @GetMapping("/themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoService.getUserByToken(token);
        BeanUtils.copyProperties(user,comment);
        comment.setUserId(user.getId());
        strategyCommentService.save(comment);
        //评论数量添加
        strategyStatisVORedisService.replynumIreplynum(comment.getStrategyId(),1);
        return JsonResult.success();
    }
    @GetMapping("/comments")
    public Object comments(StrategyCommentObjcet qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,String sid,@UserParam UserInfo userInfo){
//        String token = req.getHeader("token");
//        UserInfo user = userInfoService.getUserByToken(token);
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyStatisVORedisService.getStrategyStatisVo(sid);
        return JsonResult.success(vo);
    }
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean data =  strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(data);
    }
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean data =  strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(data);
    }
}
